﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Data;
using NPOI.HSSF.UserModel;
using System.IO;
using NPOI.HSSF.Util;

namespace Meqs.Utilities
{
    /// <summary>
    /// Excel导入导出类
    /// </summary>
    public class ExcelUtil
    {
        /// <summary>
        ///Excel导入
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static DataSet ExcelImport(Stream s)
        {
            HSSFWorkbook wb = new HSSFWorkbook(s);
            DataSet ds = new DataSet();
            int k = 0;
            for (int i = 0; i < wb.NumberOfSheets; i++)
            {
                HSSFSheet sheet = wb.GetSheetAt(i);
                IEnumerator ieRow = sheet.GetRowEnumerator();
                int index = 0;
                DataTable dt = new DataTable();
                dt.TableName = wb.GetSheetName(i).Trim();

                while (ieRow.MoveNext())
                {
                    HSSFRow row = (HSSFRow)ieRow.Current;

                    IEnumerator ieCell = row.GetCellEnumerator();
                    if (index == 0)
                    {
                        while (ieCell.MoveNext())
                        {
                            HSSFCell cell = (HSSFCell)ieCell.Current;
                            switch (cell.CellType)
                            {
                                case 0:
                                    //CELL_TYPE_NUMERIC
                                    dt.Columns.Add(cell.NumericCellValue.ToString());
                                    k++;
                                    break;
                                case 1:
                                    //CELL_TYPE_STRING
                                    dt.Columns.Add(cell.StringCellValue);
                                    k++;
                                    break;
                                case 2:
                                    //CELL_TYPE_FORMULA
                                    dt.Columns.Add(cell.RichStringCellValue.ToString());
                                    k++;
                                    break;
                                case 3:
                                    //CELL_TYPE_BLANK
                                    break;
                                case 4:
                                    //CELL_TYPE_BOOLEAN
                                    dt.Columns.Add(cell.BooleanCellValue.ToString());
                                    k++;
                                    break;
                                case 5:
                                    //CELL_TYPE_ERROR
                                    break;
                            }

                        }
                    }
                    else
                    {

                        DataRow dataRow = dt.NewRow();
                        for (int j = 0; j < k; j++)
                        {
                            if (row.GetCell(j) != null)
                            {
                                switch (row.GetCell(j).CellType)
                                {
                                    case 0:
                                        //CELL_TYPE_NUMERIC
                                        dataRow[j] = row.GetCell(j).NumericCellValue;
                                        break;
                                    case 1:
                                        //CELL_TYPE_STRING
                                        dataRow[j] = row.GetCell(j).StringCellValue;
                                        break;
                                    case 2:
                                        //CELL_TYPE_FORMULA
                                        dataRow[j] = row.GetCell(j).RichStringCellValue;
                                        break;
                                    case 3:
                                        //CELL_TYPE_BLANK
                                        dataRow[j] = "";
                                        break;
                                    case 4:
                                        //CELL_TYPE_BOOLEAN
                                        dataRow[j] = row.GetCell(j).BooleanCellValue;
                                        break;
                                    case 5:
                                        //CELL_TYPE_ERROR
                                        dataRow[j] = row.GetCell(j).ErrorCellValue;
                                        break;

                                    default:
                                        dataRow[j] = row.GetCell(j).StringCellValue;
                                        break;
                                }

                            }
                            else
                            {
                                dataRow[j] = "";
                            }

                        }
                        //while (ieCell.MoveNext())
                        //{
                        //    HSSFCell cell = (HSSFCell)ieCell.Current;
                        //    switch (cell.CellType)
                        //    {
                        //        case 0:
                        //            //CELL_TYPE_NUMERIC
                        //            dataRow[j] = cell.NumericCellValue;
                        //            break;
                        //        case 1:
                        //            //CELL_TYPE_STRING
                        //            dataRow[j] = cell.StringCellValue;
                        //            break;
                        //        case 2:
                        //            //CELL_TYPE_FORMULA
                        //            dataRow[j] = cell.RichStringCellValue;
                        //            break;
                        //        case 3:
                        //            //CELL_TYPE_BLANK
                        //            dataRow[j] = "";
                        //            break;
                        //        case 4:
                        //            //CELL_TYPE_BOOLEAN
                        //            dataRow[j] = cell.BooleanCellValue;
                        //            break;
                        //        case 5:
                        //            //CELL_TYPE_ERROR
                        //            dataRow[j] = cell.ErrorCellValue;
                        //            break;

                        //        default:
                        //            dataRow[j] = cell.StringCellValue;
                        //            break;
                        //    }
                        //    j++;
                        //}
                        dt.Rows.Add(dataRow);
                    }
                    index++;
                }
                ds.Tables.Add(dt);
            }
            return ds;
        }


        /// <summary>
        ///Excel导入
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static DataSet ExcelImport1(Stream s)
        {
            HSSFWorkbook wb = new HSSFWorkbook(s);
            DataSet ds = new DataSet();
            int k = 0;
            for (int i = 0; i < wb.NumberOfSheets; i++)
            {
                HSSFSheet sheet = wb.GetSheetAt(i);
                IEnumerator ieRow = sheet.GetRowEnumerator();
                int index = 0;
                DataTable dt = new DataTable();
                dt.TableName = wb.GetSheetName(i).Trim();

                while (ieRow.MoveNext())
                {
                    HSSFRow row = (HSSFRow)ieRow.Current;

                    IEnumerator ieCell = row.GetCellEnumerator();
                    if (index == 0)
                    {
                        while (ieCell.MoveNext())
                        {
                            HSSFCell cell = (HSSFCell)ieCell.Current;
                            switch (cell.CellType)
                            {
                                case 0:
                                    //CELL_TYPE_NUMERIC
                                    dt.Columns.Add(cell.NumericCellValue.ToString());
                                    k++;
                                    break;
                                case 1:
                                    //CELL_TYPE_STRING
                                    dt.Columns.Add(cell.StringCellValue);
                                    k++;
                                    break;
                                case 2:
                                    //CELL_TYPE_FORMULA
                                    dt.Columns.Add(cell.RichStringCellValue.ToString());
                                    k++;
                                    break;
                                case 3:
                                    //CELL_TYPE_BLANK
                                    break;
                                case 4:
                                    //CELL_TYPE_BOOLEAN
                                    dt.Columns.Add(cell.BooleanCellValue.ToString());
                                    k++;
                                    break;
                                case 5:
                                    //CELL_TYPE_ERROR
                                    break;
                            }

                        }
                    }
                    else
                    {

                        DataRow dataRow = dt.NewRow();
                        for (int j = 0; j < k; j++)
                        {
                            if (row.GetCell(j) != null)
                            {
                                switch (row.GetCell(j).CellType)
                                {
                                    case 0:
                                        ////CELL_TYPE_NUMERIC
                                        //dataRow[j] = row.GetCell(j).NumericCellValue;
                                        DateTime? datetime = null;
                                        try
                                        {
                                            datetime = row.GetCell(j).DateCellValue;
                                        }
                                        catch (Exception)
                                        {
                                            datetime = null;
                                        }
                                        if (datetime == null)
                                        {
                                            dataRow[j] = row.GetCell(j).NumericCellValue;
                                        }
                                        else
                                        {

                                            dataRow[j] = Convert.ToDateTime(datetime).ToShortDateString();
                                        }
                                        break;
                                    case 1:
                                        //CELL_TYPE_STRING
                                        dataRow[j] = row.GetCell(j).StringCellValue;
                                        break;
                                    case 2:
                                        //CELL_TYPE_FORMULA
                                        dataRow[j] = row.GetCell(j).RichStringCellValue;
                                        break;
                                    case 3:
                                        //CELL_TYPE_BLANK
                                        dataRow[j] = "";
                                        break;
                                    case 4:
                                        //CELL_TYPE_BOOLEAN
                                        dataRow[j] = row.GetCell(j).BooleanCellValue;
                                        break;
                                    case 5:
                                        //CELL_TYPE_ERROR
                                        dataRow[j] = row.GetCell(j).ErrorCellValue;
                                        break;

                                    default:
                                        dataRow[j] = row.GetCell(j).StringCellValue;
                                        break;
                                }

                            }
                            else
                            {
                                dataRow[j] = "";
                            }

                        }
                        //while (ieCell.MoveNext())
                        //{
                        //    HSSFCell cell = (HSSFCell)ieCell.Current;
                        //    switch (cell.CellType)
                        //    {
                        //        case 0:
                        //            //CELL_TYPE_NUMERIC
                        //            dataRow[j] = cell.NumericCellValue;
                        //            break;
                        //        case 1:
                        //            //CELL_TYPE_STRING
                        //            dataRow[j] = cell.StringCellValue;
                        //            break;
                        //        case 2:
                        //            //CELL_TYPE_FORMULA
                        //            dataRow[j] = cell.RichStringCellValue;
                        //            break;
                        //        case 3:
                        //            //CELL_TYPE_BLANK
                        //            dataRow[j] = "";
                        //            break;
                        //        case 4:
                        //            //CELL_TYPE_BOOLEAN
                        //            dataRow[j] = cell.BooleanCellValue;
                        //            break;
                        //        case 5:
                        //            //CELL_TYPE_ERROR
                        //            dataRow[j] = cell.ErrorCellValue;
                        //            break;

                        //        default:
                        //            dataRow[j] = cell.StringCellValue;
                        //            break;
                        //    }
                        //    j++;
                        //}
                        dt.Rows.Add(dataRow);
                    }
                    index++;
                }
                ds.Tables.Add(dt);
            }
            return ds;
        }
        /// <summary>
        /// 导出excel
        /// </summary>
        /// <param name="exportpath">导出路径</param>
        /// <param name="titleNum">标题头行数</param>
        /// <param name="buttomnum">底部行数</param>
        /// <param name="cells">数据集合</param>
        /// <returns>是否成功</returns>
        public static bool ExcelExport(string exportpath, int titleNum, int buttomnum, ExcelTableCell[][] cells)
        {
            try
            {

            }
            catch
            {
                return false;
            }
            return true;
        }




        /// <summary>
        /// 导出excel
        /// </summary>
        /// <param name="exportpath">导出路径</param>
        /// <param name="titleNum">标题头行数</param>
        /// <param name="buttomnum">底部行数</param>
        /// <param name="cells">数据集合</param>
        /// <returns>是否成功</returns>
        public static bool ExcelExport(string exportpath, string sheetname, int titleNum, int buttomnum, ExcelTableCell[][] cells)
        {
            IList<ExcelTableCell[][]> list = new List<ExcelTableCell[][]>();
            list.Add(cells);

            return ExcelExport(exportpath, new string[] { sheetname }, titleNum, buttomnum, list);
        }



        /// <summary>
        /// 导出excel
        /// </summary>
        /// <param name="exportpath">导出路径</param>
        /// <param name="titleNum">标题头行数</param>
        /// <param name="buttomnum">底部行数</param>
        /// <param name="cells">数据集合</param>
        /// <returns>是否成功</returns>
        public static bool ExcelExport(string exportpath, IList<string> sheetNameList, int titleNum, int buttomnum, IList<ExcelTableCell[][]> cellsList)
        {
            if (sheetNameList.Count != cellsList.Count) return false;

            try
            {
                string temp_path = exportpath;//生成的文件存放路径
                HSSFWorkbook wb = new HSSFWorkbook();

                for (int k = 0; k < sheetNameList.Count; k++)
                {
                    string sheetname = sheetNameList[k];
                    ExcelTableCell[][] cells = cellsList[k];


                    HSSFSheet sheet = wb.CreateSheet(sheetname);
                    List<string> usedSign = new List<string>();
                    for (int i = 0; i < cells.Length; i++)
                    {
                        for (int j = 0; j < cells[i].Length; j++)
                        {
                            if (!usedSign.Contains(cells[i][j].Sign))
                            {
                                HSSFRow row = sheet.CreateRow((short)i);
                                HSSFCell cell = row.CreateCell((short)j);
                                cell.SetCellValue(cells[i][j].Text);
                                cell.SetCellType(1);
                                if (titleNum > i)
                                {
                                    //表头样式
                                    HSSFCellStyle styletop = wb.CreateCellStyle();
                                    //四边框
                                    styletop.BorderLeft = (short)1;
                                    styletop.BorderTop = (short)1;
                                    styletop.BorderBottom = (short)1;
                                    styletop.BorderRight = (short)1;
                                    //styletop.BorderTop=HSSFCellStyle.BORDER_DOUBLE;
                                    styletop.FillForegroundColor = HSSFColor.GREY_25_PERCENT.index;
                                    styletop.FillPattern = HSSFCellStyle.SOLID_FOREGROUND;

                                    styletop.Alignment = GetAline(cells[i][j].Align);

                                    cell.CellStyle = styletop;
                                }
                                else if ((cells.Length - i - 1) < buttomnum)
                                {
                                    ///底部样式
                                    HSSFCellStyle stylebuttom = wb.CreateCellStyle();
                                    //四边框
                                    stylebuttom.BorderLeft = (short)1;
                                    stylebuttom.BorderTop = (short)1;
                                    stylebuttom.BorderBottom = (short)1;
                                    stylebuttom.BorderRight = (short)1;
                                    HSSFFont buttom = wb.CreateFont();
                                    buttom.Boldweight = HSSFFont.BOLDWEIGHT_BOLD;
                                    stylebuttom.SetFont(buttom);
                                    stylebuttom.Alignment = GetAline(cells[i][j].Align);
                                    cell.CellStyle = stylebuttom;
                                }
                                else
                                {
                                    ///值样式
                                    HSSFCellStyle stylevalue = wb.CreateCellStyle();
                                    //四边框
                                    stylevalue.BorderLeft = (short)1;
                                    stylevalue.BorderTop = (short)1;
                                    stylevalue.BorderBottom = (short)1;
                                    stylevalue.BorderRight = (short)1;
                                    stylevalue.WrapText = true;
                                    stylevalue.Alignment = GetAline(cells[i][j].Align);
                                    if (cells[i][j].Color == "Red")
                                    {
                                        HSSFFont buttom = wb.CreateFont();
                                        buttom.Color = HSSFFont.COLOR_RED;
                                        stylevalue.SetFont(buttom);
                                    }
                                    cell.CellStyle = stylevalue;
                                }

                                int start = i;
                                int end = j;
                                GetEndPoint(i, j, ref start, ref end, cells);
                                sheet.AddMergedRegion(new Region(i, j, start, end));
                                usedSign.Add(cells[i][j].Sign);

                            }
                            else
                            {
                                HSSFRow row = sheet.CreateRow((short)i);
                                HSSFCell cell = row.CreateCell((short)j);
                                cell.SetCellType(1);
                                if (titleNum > i)
                                {
                                    //表头样式
                                    HSSFCellStyle styletop = wb.CreateCellStyle();
                                    //四边框
                                    styletop.BorderLeft = (short)1;
                                    styletop.BorderTop = (short)1;
                                    styletop.BorderBottom = (short)1;
                                    styletop.BorderRight = (short)1;
                                    //styletop.BorderTop=HSSFCellStyle.BORDER_DOUBLE;
                                    styletop.FillForegroundColor = HSSFColor.GREY_25_PERCENT.index;
                                    styletop.FillPattern = HSSFCellStyle.SOLID_FOREGROUND;

                                    styletop.Alignment = GetAline(cells[i][j].Align);

                                    cell.CellStyle = styletop;
                                }
                                else if ((cells.Length - i - 1) < 1)
                                {
                                    ///底部样式
                                    HSSFCellStyle stylebuttom = wb.CreateCellStyle();
                                    //四边框
                                    stylebuttom.BorderLeft = (short)1;
                                    stylebuttom.BorderTop = (short)1;
                                    stylebuttom.BorderBottom = (short)1;
                                    stylebuttom.BorderRight = (short)1;
                                    HSSFFont buttom = wb.CreateFont();
                                    buttom.Boldweight = 1;
                                    buttom.Color = HSSFFont.COLOR_RED;
                                    stylebuttom.SetFont(buttom);
                                    stylebuttom.Alignment = GetAline(cells[i][j].Align);

                                    cell.CellStyle = stylebuttom;
                                }
                                else
                                {
                                    ///值样式
                                    HSSFCellStyle stylevalue = wb.CreateCellStyle();
                                    //四边框
                                    stylevalue.BorderLeft = (short)1;
                                    stylevalue.BorderTop = (short)1;
                                    stylevalue.BorderBottom = (short)1;
                                    stylevalue.BorderRight = (short)1;
                                    stylevalue.WrapText = true;
                                    stylevalue.Alignment = GetAline(cells[i][j].Align);
                                    if (cells[i][j].Color == "Red")
                                    {
                                        HSSFFont buttom = wb.CreateFont();
                                        buttom.Color = HSSFFont.COLOR_RED;
                                        stylevalue.SetFont(buttom);
                                    }
                                    cell.CellStyle = stylevalue;
                                }
                            }

                        }
                    }

                }

                FileStream file = new FileStream(temp_path, FileMode.Create);
                wb.Write(file);
                file.Close();

            }
            catch
            {
                return false;
            }
            return true;
        }


        /// <summary>
        /// 导出excel
        /// </summary>
        /// <param name="exportpath">导出路径</param>
        /// <param name="titleNum">标题头行数</param>
        /// <param name="buttomnum">底部行数</param>
        /// <param name="cells">数据集合</param>
        /// <returns>是否成功</returns>
        public static bool ExcelExport(string exportpath, string sheetname, string maincaption, string secondcaption, int titleNum, int buttomnum, ExcelTableCell[][] cells)
        {
            try
            {
                short tablestart = 0;

                string temp_path = exportpath;//生成的文件存放路径
                HSSFWorkbook wb = new HSSFWorkbook();

                HSSFSheet sheet = wb.CreateSheet(sheetname);


                if (!string.IsNullOrEmpty(maincaption))
                {
                    HSSFRow row = sheet.CreateRow(tablestart);
                    HSSFCell cell = row.CreateCell(0);
                    cell.SetCellValue(maincaption);
                    cell.SetCellType(1);

                    //表头样式
                    HSSFCellStyle styletop = wb.CreateCellStyle();
                    cell.CellStyle = styletop;
                    HSSFFont buttom = wb.CreateFont();
                    buttom.Boldweight = HSSFFont.BOLDWEIGHT_BOLD;
                    buttom.FontHeightInPoints = (short)16;
                    styletop.SetFont(buttom);

                    styletop.Alignment = HSSFCellStyle.ALIGN_CENTER;
                    sheet.AddMergedRegion(new Region(tablestart, 0, tablestart, cells[0].Length - 1));
                    tablestart++;
                }
                if (!string.IsNullOrEmpty(secondcaption))
                {
                    HSSFRow row = sheet.CreateRow(tablestart);
                    HSSFCell cell = row.CreateCell(0);
                    cell.SetCellValue(secondcaption);
                    cell.SetCellType(1);

                    //表头样式
                    HSSFCellStyle styletop = wb.CreateCellStyle();
                    cell.CellStyle = styletop;
                    sheet.AddMergedRegion(new Region(tablestart, 0, tablestart, cells[0].Length - 1));

                    tablestart++;
                }



                List<string> usedSign = new List<string>();
                for (int i = 0; i < cells.Length; i++)
                {
                    for (int j = 0; j < cells[i].Length; j++)
                    {
                        if (!usedSign.Contains(cells[i][j].Sign))
                        {
                            HSSFRow row = sheet.CreateRow(tablestart + (short)i);
                            HSSFCell cell = row.CreateCell((short)j);
                            cell.SetCellValue(cells[i][j].Text);
                            cell.SetCellType(1);
                            if (titleNum > i)
                            {
                                //表头样式
                                HSSFCellStyle styletop = wb.CreateCellStyle();
                                //四边框
                                styletop.BorderLeft = (short)1;
                                styletop.BorderTop = (short)1;
                                styletop.BorderBottom = (short)1;
                                styletop.BorderRight = (short)1;
                                //styletop.BorderTop=HSSFCellStyle.BORDER_DOUBLE;
                                styletop.FillForegroundColor = HSSFColor.GREY_25_PERCENT.index;
                                styletop.FillPattern = HSSFCellStyle.SOLID_FOREGROUND;

                                styletop.Alignment = GetAline(cells[i][j].Align);

                                cell.CellStyle = styletop;
                            }
                            else if ((cells.Length - i - 1) < buttomnum)
                            {
                                ///底部样式
                                HSSFCellStyle stylebuttom = wb.CreateCellStyle();
                                //四边框
                                stylebuttom.BorderLeft = (short)1;
                                stylebuttom.BorderTop = (short)1;
                                stylebuttom.BorderBottom = (short)1;
                                stylebuttom.BorderRight = (short)1;
                                HSSFFont buttom = wb.CreateFont();
                                buttom.Boldweight = HSSFFont.BOLDWEIGHT_BOLD;
                                stylebuttom.SetFont(buttom);
                                stylebuttom.Alignment = GetAline(cells[i][j].Align);
                                cell.CellStyle = stylebuttom;
                            }
                            else
                            {
                                ///值样式
                                HSSFCellStyle stylevalue = wb.CreateCellStyle();
                                //四边框
                                stylevalue.BorderLeft = (short)1;
                                stylevalue.BorderTop = (short)1;
                                stylevalue.BorderBottom = (short)1;
                                stylevalue.BorderRight = (short)1;
                                stylevalue.WrapText = true;
                                stylevalue.Alignment = GetAline(cells[i][j].Align);
                                cell.CellStyle = stylevalue;
                            }

                            int start = i;
                            int end = j;
                            GetEndPoint(i, j, ref start, ref end, cells);
                            sheet.AddMergedRegion(new Region(tablestart + i, j, tablestart + start, end));
                            usedSign.Add(cells[i][j].Sign);

                        }
                        else
                        {
                            HSSFRow row = sheet.CreateRow(tablestart + (short)i);
                            HSSFCell cell = row.CreateCell((short)j);
                            cell.SetCellType(1);
                            if (titleNum > i)
                            {
                                //表头样式
                                HSSFCellStyle styletop = wb.CreateCellStyle();
                                //四边框
                                styletop.BorderLeft = (short)1;
                                styletop.BorderTop = (short)1;
                                styletop.BorderBottom = (short)1;
                                styletop.BorderRight = (short)1;
                                //styletop.BorderTop=HSSFCellStyle.BORDER_DOUBLE;
                                styletop.FillForegroundColor = HSSFColor.GREY_25_PERCENT.index;
                                styletop.FillPattern = HSSFCellStyle.SOLID_FOREGROUND;

                                styletop.Alignment = GetAline(cells[i][j].Align);

                                cell.CellStyle = styletop;
                            }
                            else if ((cells.Length - i - 1) < 1)
                            {
                                ///底部样式
                                HSSFCellStyle stylebuttom = wb.CreateCellStyle();
                                //四边框
                                stylebuttom.BorderLeft = (short)1;
                                stylebuttom.BorderTop = (short)1;
                                stylebuttom.BorderBottom = (short)1;
                                stylebuttom.BorderRight = (short)1;
                                HSSFFont buttom = wb.CreateFont();
                                buttom.Boldweight = 1;
                                stylebuttom.SetFont(buttom);
                                stylebuttom.Alignment = GetAline(cells[i][j].Align);
                                cell.CellStyle = stylebuttom;
                            }
                            else
                            {
                                ///值样式
                                HSSFCellStyle stylevalue = wb.CreateCellStyle();
                                //四边框
                                stylevalue.BorderLeft = (short)1;
                                stylevalue.BorderTop = (short)1;
                                stylevalue.BorderBottom = (short)1;
                                stylevalue.BorderRight = (short)1;
                                stylevalue.WrapText = true;
                                stylevalue.Alignment = GetAline(cells[i][j].Align);
                                cell.CellStyle = stylevalue;
                            }
                        }

                    }
                }

                FileStream file = new FileStream(temp_path, FileMode.Create);
                wb.Write(file);
                file.Close();

            }
            catch
            {
                return false;
            }
            return true;
        }
        private static short GetAline(string alinement)
        {
            if (alinement.ToLower() == "center")
            {
                return HSSFCellStyle.ALIGN_CENTER;
            }
            else if (alinement.ToLower() == "left")
            {
                return HSSFCellStyle.ALIGN_LEFT;
            }
            else
            {
                return HSSFCellStyle.ALIGN_RIGHT;
            }
            return HSSFCellStyle.ALIGN_CENTER;
        }
        private static void GetEndPoint(int rowFrom, int colFrom, ref int rowTo, ref int colTo, ExcelTableCell[][] cells)
        {
            ExcelTableCell scell = cells[rowFrom][colFrom];
            for (int i = rowFrom; i < cells.Length; i++)
            {
                for (int j = colFrom; j < cells[i].Length; j++)
                {
                    if (scell.Sign == cells[i][j].Sign)
                    {
                        rowTo = i;
                        colTo = j;
                    }
                }
            }

        }
    }

   

        
    }

